﻿using Microsoft.OpenApi.Models;
using System.Reflection;

namespace EPlantApp.WebApi.Server.Extensions
{
    public static class SwaggerExt
    {
        public static void AddSwaggerExt(this IServiceCollection services)
        {
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            services.AddEndpointsApiExplorer();
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title = $"EPlantApp.Api文档",
                    Version = "v1",
                    Description = $"通用版本的CoreApi版本v1"
                });
                #region 读取xml信息
                // 使用反射获取xml文件，并构造出文件的路径
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                // 启用xml注释，该方法第二个参数启用控制器的注释，默认为false.
                options.IncludeXmlComments(xmlPath, true);
                #endregion
            });
        }

        public static void UseSwaggerExt(this WebApplication app)
        {
            app.UseSwagger();
            app.UseSwaggerUI();
        }
    }
}
